package com.hxcy.doki;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import static com.hxcy.doki.LoginActivity2.USER;

public class UserActivity extends AppCompatActivity {

    private String TAG = UserActivity.class.getSimpleName();
    User user;
    private WebView webView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);

        Intent intent = getIntent();


        user = (User) intent.getSerializableExtra(USER);

        //Log.e(TAG, "onCreate: "+user.getName()+user.getPwd());
        webView = (WebView)findViewById(R.id.wv_user);

        WebSettings webSettings = webView.getSettings();

        webSettings.setJavaScriptEnabled(true);

        webView.addJavascriptInterface(new JsInterface(user), "control");

        webView.loadUrl("file:///android_asset/user.html");

        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return false;
            }
        });
    }

    public class JsInterface {

        public User user;

        public  JsInterface(User user){
            this.user = user;
        }
        @JavascriptInterface
        public void showUser(User user) {
            Toast.makeText(UserActivity.this, user.getName(), Toast.LENGTH_SHORT).show();
        }
    }
}
